<?php
class ProfileInforForm extends Form{
	var $user_arr = array();
	function ProfileInforForm($user_arr){
		Form::Form("profile_infor");
		$this->link_js("javascript/moder_list_detail.js");
		$this->user_arr = $user_arr;	
	}
	
	function draw(){
		global $display;
		$this->beginForm();
		$items = array();
		$msg_error  = "";
		$style_bg="";
		$limit 	= "";
		$number_hits = 0;
		$paging = "";
		$ads_tbl = "phpclass_ad";
		
		$username = $this->user_arr["user_name"];
		$email = $this->user_arr["user_email"];
		//$hide_email = $this->user_arr["user_hide_email"];
		$user_id = (int)$this->user_arr["user_id"];
		$sms_activated = $this->user_arr["user_activated"];
		$user_up_item = $this->user_arr["user_up_item"];
		$gold = $this->user_arr["gold"];
		$gold_buy = $this->user_arr["gold_buy"];
		$block_time = $this->user_arr["block_time"];
		$user_level = $this->user_arr["level"];
		
		if(User::have_permit(ADMIN_USER))
		{
			$mongo_where = array(
	        	'ad_is_validated' => array('$in' => array(0,2))
	        );
		}
		else 
		{
			$mongo_where = array("ad_is_validated" => 0);
		}
		
		if($user_id){
			$mongo_where["ad_owner"] = $user_id;
		}
		
		if($this->user_arr["user_hide_email"] && $user_id!=User::id() && !User::is_admin())
		{
			$hide_email = 1;
		}
		else 
		{
			$hide_email = 0;
		}
		
		if(MONGO_ON){				
			$table = $ads_tbl;
			$RMon = RBMongo::connect();
		    $MongoAdQuery = $RMon->$table;
		    					 
		 	$resultField = array();
		 	$resultField[] = "ad_id";
		 	$resultField[] = "ad_cat_id";
		 	$resultField[] = "ad_title";
		 	$resultField[] = "ad_description";
		 	$resultField[] = "ad_date";
		 	$resultField[] = "ad_views";
		 	$resultField[] = "ad_type";
		 	$resultField[] = "ad_vip";
		 	$resultField[] = "ad_id_pcat";
		 	$resultField[] = "ad_id_subcat";
		 	$resultField[] = "id_cities";
		 	$resultField[] = "ad_ip";
		 	$resultField[] = "ad_is_validated";
		    $mongo_order = array("ad_date" => -1);
			if(DEBUG){
				$debug_where = "";
				foreach($mongo_where as $where_k => $where_v){
					$debug_where .= "'{$where_k}':{$where_v},";			
				}
				$debug_where = substr($debug_where, 0, -1);	 
				$debug_field = "";
				foreach($resultField as $f_v){
					$debug_field .= "{$f_v}:1,";
				}
				$debug_field = substr($debug_field, 0, -1);	 
				$debug_mongo_ad_query =  "db.{$table}.find({{$debug_where}},{{$debug_field}}).sort({ad_id: -1}).count()";
				RBMongo::mongo_debug($debug_mongo_ad_query);
			}
			$total = $MongoAdQuery->find($mongo_where, $resultField)->sort($mongo_order)->count();
		}
		else{
			$sql = "SELECT count(*) as count_ad FROM $ads_tbl WHERE ad_owner= $user_id AND ad_is_validated IN (0,2)";
			$result = DB::query($sql);
			$rows = mysql_fetch_array($result);
			$total = $rows["count_ad"];
		}
		if($total){
			$page_no = (int)Url::get("page_no",0)>0?Url::get("page_no"):1;
			$url_new = "member-$user_id";
			$paging = ECPaging::paging_profile($limit,$total,50,10,false,"page_no",$url_new);
			$offset = intval(($page_no-1)*50);
			
			if(MONGO_ON){
		    	//Init Mongo
		    	$table = $ads_tbl;
		    	$RMon = RBMongo::connect();
		    	$MongoAdQuery = $RMon->$table;
		    	$resultField = array();
		     	$resultField[] = "ad_id";
		     	$resultField[] = "ad_cat_id";
		     	$resultField[] = "ad_title";
		     	$resultField[] = "ad_description";
		     	$resultField[] = "ad_date";
		     	$resultField[] = "ad_views";
		     	$resultField[] = "ad_type";
		     	$resultField[] = "ad_vip";
		     	$resultField[] = "ad_id_pcat";
		     	$resultField[] = "ad_id_subcat";
		     	$resultField[] = "id_cities";
		     	$resultField[] = "ad_ip";
		     	$resultField[] = "ad_is_validated";
		        $mongo_order = array('ad_date' => -1);
		        if(DEBUG){
					$debug_where = "";
					foreach($mongo_where as $where_k => $where_v){
						
						$debug_where .= "'{$where_k}':{$where_v},";			
					}
					$debug_where = substr($debug_where, 0, -1);	 
					$debug_field = "";
					foreach($resultField as $f_v){
						$debug_field .= "{$f_v}:1,";
					}
					$debug_field = substr($debug_field, 0, -1);	 
					$debug_mongo_ad_query =  "db.{$table}.find({{$debug_where}},{{$debug_field}}).sort({ad_id: -1}).skip($offset).limit(50)";
					RBMongo::mongo_debug($debug_mongo_ad_query);
				}
		        
		        $adCursor = $MongoAdQuery->find($mongo_where, $resultField)->sort($mongo_order)->skip($offset)->limit(50);
				while ($row = $adCursor->getNext()){
					$items[$row["ad_id"]] = $row;
				}
			}
			else{
				$sql = "SELECT * from $ads_tbl WHERE ad_owner = $user_id AND ad_is_validated IN(0,2) ORDER BY ad_id DESC $limit";
				$result = DB::query($sql);
				while ($row = mysql_fetch_assoc($result)){
					$items[$row["ad_id"]] = $row;
				}
				mysql_free_result($result);
			}
			if($items){
				foreach($items as $row){
					$style_bg = ($style_bg=="Odd") ? "Even" : "Odd";
					$row["style_bg"] = $style_bg;
				 	$row["ad_title"] = ECLib::word_limit($row["ad_title"],8,"...");				 	
				 	$row["rbcatname"] = @CGlobal::$all_cats[$row["ad_cat_id"]]["rbname"];
				 	$row["ad_date"] = ECLib::formatDateShort($row["ad_date"]);
					$row["rbname"] = ECLib::safe_title(substr($row["ad_title"], 0, 50));
			
			        //Nếu là mode thì view hết nội dung tooltip
			       if(User::is_mod()){
			            $row["ad_description"] = ECLib::remove_4_js(ECLib::remove_tag(ECLib::post_db_parse_html($row["ad_description"])));
			        }
			        else{
			            $row["ad_description"] = ECLib::remove_4_js(ECLib::word_limit(ECLib::remove_tag(ECLib::post_db_parse_html($row["ad_description"])),50,"..."));
			        }
					$row["city"] = CGlobal::$all_cities[$row["id_cities"]]["name"]; 		
					$row["c"] = intval($row["ad_cat_id"]);
					$row["rbname"] = ECLib::safe_title($row["ad_title"]);
				 	$row["url"] = WEB_DIR.CGlobal::$all_cities[$row["id_cities"]]["rbname"]."/{$row["rbname"]}-c{$row["c"]}-raovat-{$row["ad_id"]}.html";
				 	
				 	$row["ad_cat_name"] = @CGlobal::$all_cats[$row["ad_cat_id"]]["cat_name"];
				 	$row["c_url"] = WEB_DIR.CGlobal::$all_cities[$row["id_cities"]]["rbname"]."/".@CGlobal::$all_cats[$row["ad_cat_id"]]["rbname"].(($row["ad_cat_id"]) ? "-c{$row["ad_cat_id"]}":"").".html";
				 	
					$items[$row["ad_id"]]=$row;
				}
			}
			
		}
		
		if (isset($user_id) && $user_id > 0)
		{
			$sql = "SELECT id, name, tax_code, phone, address, city FROM phpclass_company WHERE user_id={$user_id} AND active=1 LIMIT 1";
			$company = DB::fetch($sql);
			if ($company)
			{
				$company['city_name'] = CGlobal::$all_cities[$company['city']]['name'];
			}
			
			$display->add("company",$company);
		}
			
		$display->add("items",$items);
		$display->add("paging",$paging);
		$display->add("is_moder",User::is_mod());
		$display->add("is_admin",User::is_admin());
		$display->add("msg_error",$msg_error);
		$display->add("NUMBER_OF_ADS",$total);
		$display->add("ID_ACC",$user_id);
		$display->add("vip_name",CGlobal::$acc_vip_info['name'][$this->user_arr['level']]);
		$display->add("user_up_item",$user_up_item);
		$display->add("user_id_view",User::id());
		$display->add("gold",$gold);
		$display->add("gold_buy",$gold_buy);
		$display->add("USERNAME",$username);
		$display->add("hide_email",$hide_email);
		$display->add("USER_EMAIL",$email);
		$display->add("USER_ID",$user_id);
		$display->add("is_block",($block_time>TIME_NOW||$block_time==-1)?true:false);
		$display->add("SMS_ACTIVATED",$sms_activated);
		$display->add("user_color",CGlobal::$acc_vip_info['color'][$user_level]);
		
		$display->output("ProfileInfor");
		$this->endForm();
	}
}
?>
